package rmi;

import heap.ResultRow;
import heap.ResultSchema;


import java.util.*; 
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import SQL.plan.Plan;
import SQL.planner.Planner;
import SQL.query.SQL;
import SQL.scan.Scan;



@SuppressWarnings("serial") 
public class RemoteResultSetImpl extends UnicastRemoteObject implements RemoteResultSet {
	private Plan plan ; 
	private Scan s;
	private ResultSchema sch ; 
	private ResultRow row ;  
	private RemoteConnectionImpl rconn;
	private SQL qry = null ; 
@Override
public Scan getScan() throws RemoteException {
	// TODO Auto-generated method stub
	return s;
}
	public RemoteResultSetImpl( Plan plan, RemoteConnectionImpl rconn, SQL qry2 ) throws RemoteException, Exception {
		this.plan = plan ; 
		s = plan.open();
		sch = s.getSchema();
		this.rconn = rconn;
		this.qry = qry2 ; 
	}

	public boolean next() throws RemoteException {
		try {
			if ( (row = s.next()) != null ) return true;
			else return false ;
		} catch ( Exception e) {
			return false ;
		}
	}

	public int getInt(String fldname) throws RemoteException {
		try {
			return row.getInt(fldname, sch );
		}catch ( Exception e ) { 
			throw new RemoteException ( "error occur when getInt from a row") ; 
		}
	}

	public String getString(String fldname) throws RemoteException {
		try {
			return row.getString(fldname, sch );
		}catch ( Exception e ) { 
			throw new RemoteException ( "error occur when getString from a row") ; 
		}
	}

	public RemoteMetaData getMetaData() throws RemoteException {
		return new RemoteMetaDataImpl(sch);
	}

	public void close() throws RemoteException {
		rconn.commit();
		try { 
			plan.close() ;
		}catch ( Exception e ) {
			throw new RemoteException ( "error when close resultset" ) ; 
		}
	}

	public int getInt(int index) throws RemoteException {
		return Integer.valueOf(row.get(index - 1 ));
	}

	public String getString(int index) throws RemoteException {
		return row.get(index -1 ) ;
	}
}

